//
//  WBComposeBottomView.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/17.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit

// MARK: - 设置按钮的枚举
enum WBComposeBottomViewButtonType: Int{
    
    case Picture = 0
    case Mention = 1
    case Trend = 2
    case Emoticon = 3
    case Add = 4

}


class WBComposeBottomView: UIView {
    
    var emoticonBtn:UIButton?
    
    // MARK: - 定义属性 修改内部图片
    var isEmoticon:Bool = false{
        //传递给我是true的时候
        didSet{
            //要赋值的图片名字 // compose_keyboardbutton_background_highlighted
            //compose_emoticonbutton_background_highlighted
            
            let imageName: String
            
            if isEmoticon{
                //是系统键盘 按钮图片转变为笑脸
                imageName = "compose_emoticonbutton_background"
            
            }else{
                //是自定义键盘 按钮图片转变为系统键盘
                imageName = "compose_keyboardbutton_background"
                
            }
            
            emoticonBtn?.setImage(UIImage(named: imageName), forState: .Normal)
            emoticonBtn?.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        
        }
    
    }
    
    // MARK: - 定义一个闭包 告诉外界按钮被点击了
    var closure:((type:WBComposeBottomViewButtonType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    // MARK: - 设置UI
    func setupUI() {
        
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        
        //设置约束
        let pictureBtn = addChildButton("compose_toolbar_picture",tag: .Picture)
        let mentionBtn = addChildButton("compose_mentionbutton_background",tag: .Mention)
        let trendBtn = addChildButton("compose_trendbutton_background",tag: .Trend)
        emoticonBtn = addChildButton("compose_emoticonbutton_background",tag: .Emoticon)
        let addBtn = addChildButton("compose_add_background",tag: .Add)
        
        pictureBtn.snp_makeConstraints { (make) in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionBtn)
        }
        mentionBtn.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureBtn.snp_trailing)
            make.width.equalTo(trendBtn)
        }
        trendBtn.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionBtn.snp_trailing)
            make.width.equalTo(emoticonBtn!)
        }
        emoticonBtn!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendBtn.snp_trailing)
            make.width.equalTo(addBtn)
        }
        addBtn.snp_makeConstraints { (make) in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(emoticonBtn!.snp_trailing)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加按钮 传给我一个图片
    func addChildButton(imageName: String , tag:WBComposeBottomViewButtonType) -> UIButton{
        
        //创建button
        let button = UIButton()
        
        //设置按钮的tag值
        button.tag = tag.rawValue
        
        //添加按钮点击事件
        button.addTarget(self, action: #selector(WBComposeBottomView.btnClick(_:)), forControlEvents: .TouchUpInside)
        
        //设置背景图片 compose_add_background_highlighted
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        //添加控件
        addSubview(button)
        
        return button
    }
    
    // MARK: - 监听按钮点击事件
    func btnClick(button:UIButton) {
        //执行闭包 告诉外接按钮被点击了
        closure?(type:WBComposeBottomViewButtonType(rawValue: button.tag)!)
    }

}
